package io.renren.modules.pachong.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.job.service.ScheduleJobService;
import io.renren.modules.pachong.Funny;
import io.renren.modules.pachong.service.FunnyService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.shiro.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 搞笑模块
 *
 * @author Peter
 * @date 2018-8-7
 */
@RestController
@RequestMapping("/sys/funny")
public class FunnyController {
    @Autowired
    private FunnyService funnyService;

    /**
     * 新增 快资讯 的数据
     * 类型 1：搞笑，2：情感，3：社会，4：房产，5：汽车，6：科技，7：体育，8：历史，9：星闻，10：育儿
     *
     * @param file
     * @return
     */
    @ApiOperation("新增快资讯数据:")
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @ResponseBody
    public R addKuaiZhiXunFunnyData(MultipartFile file, Integer type) {
        Subject subject = ShiroUtils.getSubject();
        SysUserEntity user = (SysUserEntity) subject.getPrincipals().getPrimaryPrincipal();
        if (user == null || user.getUserId() != 1) {
            return R.error("无权限");
        }
        if (file == null || file.isEmpty() || file.getSize() <= 0) {
            return R.error("文件不能为空");
        }
        if (type == null) {
            return R.error("错误");
        }
        try {
            funnyService.addNewFunnyData(file.getInputStream(), type);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    @ApiOperation("列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public R funnyList(@RequestParam Map<String, Object> params) {
        PageUtils page = funnyService.queryPage(params);
        return R.ok().put("page", page);
    }


}
